import BaseSingleClass from "./BaseSingleClass";
import { LOCAL_KEY } from "./Const";

export default class LocalData extends BaseSingleClass{
    gold_coin=0
    signday=-1
    /** 最后签到日期 */
    last_sign_day=''
    init():void{
        const str=localStorage.getItem(LOCAL_KEY)
        if(str){
            const json=JSON.parse(str)
            this.gold_coin=json.gold_coin
            if(json.signday)this.signday=json.signday
            this.last_sign_day=json.last_sign_day||''
        }
        this.save()
    }
    sign():void{
        const today=new Date().Format('yyyy-MM-dd')
        if(today==this.last_sign_day)return
        this.signday++
        this.last_sign_day=today
        this.save()
    }
    /**
     * 保存游戏数据
     */
    save():void{
        localStorage.setItem(LOCAL_KEY, JSON.stringify(this))
    }
}